package com.teamwork;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class tkzj {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入选择题ID ：");
        String cHID = scanner.nextLine();
        System.out.print("请输入学科ID：");
        String cno = scanner.nextLine();
        System.out.print("请输入题目内容：");
        String item = scanner.nextLine();
        System.out.print("请输入选项A：");
        String cHA = scanner.nextLine();
        System.out.print("请输入选项B：");
        String cHB = scanner.nextLine();
        System.out.print("请输入选项C：");
        String cHC = scanner.nextLine();
        System.out.print("请输入选项D：");
        String cHD = scanner.nextLine();
        System.out.print("请输入正确答案：");
        String cHKey = scanner.nextLine();

        scanner.close();

        addChoice(cHID ,cno, item, cHA, cHB , cHC , cHD , cHKey);
    }

    public static void addChoice(String cHID, String cno, String item, String cHA, String cHB , String cHC , String cHD , String cHKey) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        try {

            // 加载数据库驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 获取数据库连接
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentexam", "root", "123456");

            // 准备SQL语句
            String sql = "INSERT INTO choice (CHID, Cno, item, CHA, CHB, CHC, CHD, CHkey) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";

            // 创建PreparedStatement对象
            preparedStatement = connection.prepareStatement(sql);

            // 设置参数
            preparedStatement.setString(1, cHID); // 修改这一行，设置学生ID参数
            preparedStatement.setString(2, cno );
            preparedStatement.setString(3, item);
            preparedStatement.setString(4, cHA);
            preparedStatement.setString(5, cHB);
            preparedStatement.setString(6, cHC);
            preparedStatement.setString(7, cHD);
            preparedStatement.setString(8, cHKey);

            // 执行SQL语句
            int rowsAffected = preparedStatement.executeUpdate();

            // 输出结果
            if (rowsAffected > 0) {
                System.out.println("试题信息添加成功！");
            } else {
                System.out.println("试题信息添加失败！");
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
